package com.qf.java2102.qihaonan.hotel.controller.order;

import com.qf.java2102.qihaonan.hotel.entity.ResultVO;
import com.qf.java2102.qihaonan.hotel.service.IOrderService;
import com.qf.java2102.qihaonan.hotel.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author qhn
 * @version 1.0
 */
@WebServlet("/order/pay")
public class OrderPayController extends HttpServlet {

    private IOrderService orderService = new OrderServiceImpl();

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderId = request.getParameter("orderId");
        String moneyStr = request.getParameter("money");
        BigDecimal money = new BigDecimal(moneyStr);
        ResultVO<String> resultVO = orderService.pay(orderId, money);
        if(resultVO.getSuccess()) {
            response.getWriter().write("<script>alert('" + resultVO.getMessage() + "');location.href='" + request.getContextPath()  +"/login.jsp'</script>");
        } else {
            response.getWriter().write("<script>alert('" + resultVO.getMessage() + "');</script>");
        }

    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
